package com.polaris.live.ui.message.widget.single_message

import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.adapter.MessageAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.databinding.MessageItemGiftBinding
import com.polaris.live.im.db.entity.MessageEntity
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.normal

/**
 * MessageGiftItemView
 *
 * @author Created by lucas on 2023/10/21 15:08
 * @since 1.0.0
 */
class MessageGiftItemHolder(
    adapter: MessageAdapter,
    binding: MessageItemGiftBinding
) : AbstractMessageItemHolder<MessageItemGiftBinding>(adapter, binding) {

    override fun bind(item: MessageEntity) {
        mBinding.apply {
            bindCommon(
                item,
                layoutTime.messageTextTime,
                layoutLeft,
                layoutRight,
                layoutLeftAvatar.avAvatar,
                layoutRightAvatar.avAvatar,
                layoutLeftAvatar.avFv,
                layoutRightAvatar.avFv,
                bubbleLeft,
                bubbleRight,
                ivState,
                tvError
            )

            if (item.sender == UserManager.getUser().userId) {
                setRight(item)
            } else {
                setLeft(item)
            }
        }
    }

    private fun setLeft(item: MessageEntity) {
        mBinding.apply {
            messageLeftGiftText.text =
                "${StringUtils.getString(string.you_receive)} × ${item.payloadBean?.giftAmount ?: 1}"

            Glide.with(messageLeftGiftImg.context)
                .load(ImageUtils.imgPath(item.extBean?.giftUrl))
                .normal()
                .into(messageLeftGiftImg)
        }
    }

    private fun setRight(item: MessageEntity) {
        mBinding.apply {
            Glide.with(messageRightGiftImg.context)
                .load(ImageUtils.imgPath(item.extBean?.giftUrl))
                .normal()
                .into(messageRightGiftImg)

            messageRightGiftTextNum.text = "×${item.payloadBean?.giftAmount ?: 1}"
        }
    }
}